import type { User } from './UserType'

export type DataBase = {
  users: Record<string, User>;
  getUserByName(name: string): User | undefined;
  addUser(user: User): boolean;
}

export const dataBase: DataBase = {
    users: {
        Jack: {
            name: 'Jack',
            email: '123123@123.com',
            password: '123456'
        },
        Rose: {
            name: 'Rose',
            email: '321321@123.com',
            password: '654321'
        }
    },
    getUserByName (name: string) {
        return this.users[name]
    },
    addUser (user: User) {
        // 已存在的用户不允许再次注册
        if (this.getUserByName(user.name) !== undefined) {
            return false
        }
        // 添加用户
        this.users[user.name] = {
            name: user.name,
            email: user.email,
            password: user.password
        }
        return true
    }
}
